package cz.mff.kusad5am.hb.ws;

import java.util.List;

import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.xpath.XPath;
import org.springframework.ws.server.endpoint.AbstractJDomPayloadEndpoint;

import cz.mff.kusad5am.hb.schema.list.Hostel;
import cz.mff.kusad5am.hb.schema.roomlist.RoomType;
import cz.mff.kusad5am.hb.service.ListOfRoomsWorker;

public class ListOfRoomsEndpoint extends AbstractJDomPayloadEndpoint {

	private final ListOfRoomsWorker worker;
	
	public ListOfRoomsEndpoint(ListOfRoomsWorker worker) {
		this.worker = worker;
	}
	
	
	@Override
	protected Element invokeInternal(Element requestElement) throws Exception {
		
		Namespace ns = requestElement.getNamespace();
		String nsp = ns.getPrefix();
		String country = XPath.newInstance("//"+nsp+":RoomListRequest/@Country").valueOf(requestElement);
		String city = XPath.newInstance("//"+nsp+":RoomListRequest/@City").valueOf(requestElement);
		String start = XPath.newInstance("//"+nsp+":RoomListRequest/@arrivalDate").valueOf(requestElement);
		String guests = XPath.newInstance("//"+nsp+":RoomListRequest/@numberOfGuests").valueOf(requestElement);
		String nights = XPath.newInstance("//"+nsp+":RoomListRequest/@numerOfNights").valueOf(requestElement);
		String hostelId = XPath.newInstance("//"+nsp+":RoomListRequest/"+nsp+":ChosenHostel/@id").valueOf(requestElement);
		
		List<RoomType> types = worker.getListOfRooms(hostelId);
		Hostel hostel = worker.getHostel(hostelId);
		Element response = createResponse(types, country, city, start, guests, nights, hostel, ns);
		return response;
	}
	
	private Element createResponse(List<RoomType> types, String country, String city, String start, String guests, String nights, Hostel hostel, Namespace tns) {
		Element response = new Element("RoomListResponse");
		response.setNamespace(tns);
		response.setAttribute("Country", country);
		response.setAttribute("City", city);
		response.setAttribute("arrivalDate", start);
		response.setAttribute("numberOfGuests", guests);
		response.setAttribute("numerOfNights", nights);
		
		Element hostelWithRooms = new Element("HostelWithRooms");
		hostelWithRooms.setNamespace(tns);
		hostelWithRooms.setAttribute("name", hostel.getName());
		hostelWithRooms.setAttribute("id", new Integer(hostel.getId()).toString());
		hostelWithRooms.setAttribute("Country", hostel.getCountry());
		hostelWithRooms.setAttribute("City", hostel.getCity());
		hostelWithRooms.setAttribute("Address", hostel.getAddress());
		hostelWithRooms.setAttribute("ZipCode", hostel.getZipCode());
		response.setContent(hostelWithRooms);
		
		for(RoomType r: types) {
			Element child = new Element("RoomType");
			child.setNamespace(tns);
			child.setAttribute("isPrivate", r.getIsPrivate());
			child.setAttribute("bedCount", r.getBedCount());
			child.setAttribute("hasPrivateShower", r.getHasPrivateShower());
			child.setAttribute("note", r.getNote());
			child.setAttribute("priceForNightForPerson", r.getPriceForNightForPerson());
			child.setAttribute("id", r.getId());
			hostelWithRooms.addContent(child);
		}
		return response;
	}
}
